<?php

namespace App\Http\Controllers\Admin;

use App\Models\AwardItem;
use Illuminate\Http\Request;

class AwardItemController extends BaseController
{
    //
    public function list(Request $request)
    {
        $data = AwardItem::query()->with('award')->get()->toArray();

        return $this->success($data);
    }

    public function edit(int $id, Request $request)
    {
        $request->validate([
            'name' => 'required|max:10|min:2',
            'amount' => 'required|numeric',
            'round' => 'required|numeric',
            'award_id' => 'required',
        ]);

        $post = $request->post();

        $awardItem = AwardItem::query()->findOrNew($id);

        $awardItem->name = $post['name'];
        $awardItem->amount = $post['amount'];
        $awardItem->round = $post['round'];
        $awardItem->award_id = $post['award_id'];
        $awardItem->status = 0;

        $awardItem->save();

        return $this->success($awardItem);
    }

    public function detail(int $id)
    {
        $data = AwardItem::query()->find($id)->toArray();

        return $this->success($data);
    }
}
